﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.Class;
using WindowsForms.service;

namespace WindowsForms
{
    public partial class UnitManagement : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        List<Units> lsUnit;

        public UnitManagement()
        {
            iQuery = new Service1();
            InitializeComponent();
            GetAllUnit();
        }

        private void GetAllUnit()
        {
            ConvertToList convert = new ConvertToList();
            lsUnit = convert.ConvertTolistUnits(iQuery.SelectUnitsAll().Tables[0]);
            gridControl.DataSource = lsUnit;
            btnDelUnit.Enabled = false;
            btnEditUnit.Enabled = false;
        }

        private void btnAddUnit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (UnitProcess unitProcess = new UnitProcess())
            {
                if (unitProcess.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    Units unitObj = new Units();
                    unitObj.UnitName = unitProcess.Unit_Name;
                    unitObj.Description = unitProcess.Unit_Des;
                    iQuery.InsertUnit(unitObj.UnitName, unitObj.Description);
                    lsUnit.Add(unitObj);
                    gvUnit.RefreshData();
                }
            }
        }

        private void btnEditUnit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvUnit.GetSelectedRows();
            Units unitSelected = gvUnit.GetRow(rows[rows.Length - 1]) as Units;

            using (UnitProcess frmUnit = new UnitProcess(true))
            {
                //Update unit
                frmUnit.Unit_Name = unitSelected.UnitName;
                frmUnit.Unit_Des = unitSelected.Description;

                if (frmUnit.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    unitSelected.UnitName = frmUnit.Unit_Name;
                    unitSelected.Description = frmUnit.Unit_Des;
                    iQuery.UpdateUnit(unitSelected.UnitID, unitSelected.UnitName, unitSelected.Description, unitSelected.IsDeleted);
                    gvUnit.RefreshData();
                }
            }
        }

        private void gvUnit_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            btnEditUnit.Enabled = gvUnit.SelectedRowsCount > 0;
            btnDelUnit.Enabled = gvUnit.SelectedRowsCount > 0;
        }

        private void btnDelUnit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvUnit.GetSelectedRows();
            Units unitSelected = gvUnit.GetRow(rows[rows.Length - 1]) as Units;
            string message = string.Format(Constants.MESS_CONFIRMDELETE, unitSelected.UnitName);
            DialogResult dr = MessageBox.Show(message, Constants.MESS_DELETE_CAPTION, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                iQuery.DeleteUnit(unitSelected.UnitID);
                lsUnit.Remove(unitSelected);
                gvUnit.RefreshData();
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GetAllUnit();
        }

        private void UnitManagement_FormClosing(object sender, FormClosingEventArgs e)
        {
            btnDelUnit.Enabled = gvUnit.SelectedRowsCount > 0;
            btnEditUnit.Enabled = gvUnit.SelectedRowsCount > 0;
        }
    }
}